package com.guidance.guidance.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.guidance.guidance.domain.Message;
import com.guidance.guidance.domain.Project;
import com.guidance.guidance.repository.ProjectRepository;

@RestController
@RequestMapping("/project")
public class CreateProjectController {
	
	@Autowired
	private ProjectRepository projectRepository;
	
	/**
	 * 创建项目内容接口，客户端使用时需要传入五个参数，分别是projectName、join_num、teacher、state、path
	 * @param msg
	 * @param p
	 * @return
	 */
	@RequestMapping("/createProject")
	public Message<Project> createProject(Message<Project> msg , Project p) {
		List<Project> projectList = projectRepository.findAll();
		for(Project pj : projectList) {
			if(pj.getProjectName().equals(p.getProjectName())) {
				msg.setCode("404");
				msg.setMsg("该项目已存在");
				msg.setSuccess("false");
				return msg;
			}
		}
		if(projectRepository.findByProjectName(p.getProjectName()) != null) {
			msg.setCode("404");
			msg.setMsg("项目已存在");
			msg.setSuccess("false");
			return msg;
		}
		Project pp = projectRepository.save(p);
		if(pp != null) {
			msg.setCode("200");
			msg.setMsg("创建成功");
			msg.setSuccess("true");
			msg.setData(p);
		}else {
			msg.setCode("404");
			msg.setMsg("创建失败");
			msg.setSuccess("false");
		}
		return msg;
	}
	
	/**
	 * 获取数据库中所有的项目信息
	 * @param msg
	 * @return
	 */
	@RequestMapping("/getAllProject")
	public Message<List> getAllProject(Message<List> msg ){
		List<Project> projectList = projectRepository.findAll();
		if(projectList.size()!=0&&projectList!=null) {
			msg.setCode("200");
			msg.setMsg("获取成功");
			msg.setSuccess("true");
			msg.setData(projectList);
		}else {
			msg.setCode("404");
			msg.setMsg("获取失败");
			msg.setSuccess("false");
		}
		return msg;
	}
	
	
}
